<template>
  <div>
    <el-button type="primary" @click.prevent="toAdd()">添加收货地址</el-button>
    <el-table :data="pageInfo.list" border style="width: 100%">
      <el-table-column label="序号" width="50">
        <template slot-scope="scope">
          {{ (form.pageNum - 1) * form.pageSize + scope.$index + 1 }}
          <!-- 从 1 开始计数 -->
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="收货地址"
        width="300"
      ></el-table-column>
      <el-table-column prop="remark" label="备注" width="200"></el-table-column>
      <el-table-column prop="isDefault" label="状态" width="80">
        <template slot-scope="scope">
          {{ scope.row.isDefault === 1 ? "默认地址" : "" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="280">
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              v-if="scope.row.isDefault == 0"
              type="primary"
              size="small"
              @click="setDefaultAddress(scope.row.id)"
              >设为默认</el-button
            >
            <el-button
              v-if="scope.row.isDefault == 1"
              type="primary"
              size="small"
              @click="setDefaultAddress(scope.row.id)"
              >取消默认</el-button
            >
            <el-button
              type="primary"
              size="small"
              @click="
                $router.push({
                  name: 'addressEdit',
                  query: { id: scope.row.id },
                })
              "
              >编辑</el-button
            >
            <el-button
              v-if="scope.row.isDefault == 0"
              type="primary"
              size="small"
              @click="delAddress(scope.row.id)"
              >删除</el-button
            >
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="changePageSize"
        @current-change="changePageNum"
        :current-page="form.pageNum"
        :page-sizes="[5, 10]"
        :page-size="form.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "addressPage",
  data() {
    return {
      pageInfo: {},
      form: {
        pageNum: 1,
        pageSize: 5,
      },
      uid: "",
      currentDefaultId: "",
    };
  },
  created() {
    const userJson = window.sessionStorage.getItem("user");
    if (userJson) {
      const user = JSON.parse(userJson);
      this.uid = user.id;
    }
  },
  methods: {
    searchInfo() {
      this.$axios
        .get("api/address/page", {
          params: {
            pageNum: this.form.pageNum,
            pageSize: this.form.pageSize,
            uid: this.uid,
          },
        })
        .then((response) => {
          this.pageInfo = response.data.pageInfo;
          console.log(response.data.pageInfo.list[0]);
          if (response.data.pageInfo.list[0].isDefault == 1) {
            this.currentDefaultId = response.data.pageInfo.list[0].id;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    toAdd() {
      if (this.pageInfo.list.length == 3) {
        alert("收货地址最多只能设置3个");
        return;
      }
      this.$router.push("/managerIndex/manageAddress/add");
    },
    changePageNum(newPageNum) {
      this.form.pageNum = newPageNum;
      this.searchInfo();
    },
    changePageSize(newPageSize) {
      this.form.pageSize = newPageSize;
      this.searchInfo();
    },
    delAddress(id) {
      if (confirm("确认删除该收货地址吗？")) {
        this.$axios
          .get(`api/address/del/${id}`)
          .then((response) => {
            if (response.data.delStatus) {
              alert("删除成功");
              this.searchInfo();
            }
          })
          .catch((error) => {
            this.$message.error("删除失败");
            console.error(error);
          });
      }
    },
    setDefaultAddress(id) {
      let newDefaultId = id;
      if (id == this.currentDefaultId) {
        newDefaultId = "";
      }
      this.$axios
        .get("api/address/change", {
          params: {
            currentDefaultId: this.currentDefaultId,
            newDefaultId,
          },
        })
        .then((response) => {
          if (response.data.changeStatus) {
            alert("操作成功");
            this.currentDefaultId = newDefaultId;
            this.searchInfo();
          }
        })
        .catch((error) => {
          this.$message.error("操作失败");
          console.error(error);
        });
    },
  },
  mounted() {
    this.searchInfo();
  },
};
</script>

<style></style>
